// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.core.shadow;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.dengues.core.components.IXMLComponent;
import org.dengues.core.components.NodePropertyParameter;
import org.dengues.core.process.CompElement;
import org.dengues.core.process.ICompConnection;
import org.dengues.core.process.ICompNode;
import org.dengues.core.process.ICompProcess;
import org.dengues.core.process.ICompSubProcess;
import org.dengues.core.property.EParameterFieldCategory;
import org.dengues.core.property.EPropertyCategory;
import org.dengues.core.resource.DenguesProjectFactory;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class ShadowCompProcess implements ICompProcess, ICompSubProcess {

    private final List<ShadowCompNode> compNodes = new ArrayList<ShadowCompNode>();

    private final List<ShadowCompNode> rootNodes = new ArrayList<ShadowCompNode>();

    /**
     * Qiang.Zhang.Adolf@gmail.com ShadowCompProcess constructor comment.
     * 
     * @param rootNode
     * @param subNodes
     */
    public ShadowCompProcess(ShadowCompNode rootNode, List<ShadowCompNode> subNodes) {
        rootNodes.add(rootNode);
        compNodes.add(rootNode);
        if (subNodes != null) {
            for (ShadowCompNode shadowCompNode : subNodes) {
                compNodes.add(shadowCompNode);
            }
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com ShadowCompProcess constructor comment.
     * 
     * @param rootNode
     */
    public ShadowCompProcess(ShadowCompNode rootNode) {
        this(rootNode, null);
    }

    /**
     * this Shadow process for the zTestConnection. Qiang.Zhang.Adolf@gmail.com ShadowCompProcess constructor comment.
     * 
     * @param component
     * @param properties
     */
    public ShadowCompProcess(IXMLComponent component, Properties properties) {
        this(new ShadowCompNode(component, properties));
        for (ShadowCompNode node : compNodes) {
            node.setCompProcess(this);
        }
        addShadowParameter();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "addShadowParameter".
     */
    private void addShadowParameter() {
        NodePropertyParameter param = getRootNode().getElementParameter("ISSHADOW"); //$NON-NLS-1$
        if (param == null) {
            param = new NodePropertyParameter(getRootNode());
            param.setName("ISSHADOW"); //$NON-NLS-1$
            param.setValue("true"); //$NON-NLS-1$
            param.setDisplayName("ISSHADOW"); //$NON-NLS-1$
            param.setFieldCategory(EParameterFieldCategory.CHECK);
            param.setPropertyCategory(EPropertyCategory.PROPERTY);
            param.setReadOnly(true);
            param.setShow(false);
            param.setNbRow(1);
            getRootNode().getElementParameters().add(param);
        } else {
            param.setValue(Boolean.TRUE);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompProcess#getCompNodes()
     */
    public List<? extends ICompNode> getCompNodes() {
        return compNodes;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompProcess#getCompSubProcesses()
     */
    public List<? extends ICompSubProcess> getCompSubProcesses() {
        List<ICompSubProcess> list = new ArrayList<ICompSubProcess>();
        list.add(this);
        return list;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompProcess#getConnections()
     */
    public List<? extends ICompConnection> getConnections() {
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompProcess#getCurrentProjectName()
     */
    public String getCurrentProjectName() {
        return DenguesProjectFactory.getDenguesProject().getName();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompProcess#getDescription()
     */
    public String getDescription() {
        return "shadow process "; //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompProcess#getElements()
     */
    public List<? extends CompElement> getElements() {
        return compNodes;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompProcess#getId()
     */
    public String getId() {
        return "shadow_process"; //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompProcess#getName()
     */
    public String getName() {
        return "shadow_process"; //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompProcess#getPurpose()
     */
    public String getPurpose() {
        return "shadow_process"; //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompProcess#getRootCompNodes()
     */
    public List<? extends ICompNode> getRootCompNodes() {
        return rootNodes;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompProcess#getVersion()
     */
    public String getVersion() {
        return "0.1"; //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompProcess#isGridEnabled()
     */
    public boolean isGridEnabled() {
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompSubProcess#getRootNode()
     */
    public ICompNode getRootNode() {
        return rootNodes.get(0);
    }

}
